package com.ben.multiple.elasticsearch.template;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchClients;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

class JavaIndexTest {
    static ElasticsearchTemplate template;
    static ElasticsearchClient client;

    static {
        client = ElasticsearchClients.createImperative(ClientConfiguration.create("192.168.229.141:9200"));
        template = new ElasticsearchTemplate(client);
    }

    public static void main(String[] args) {
        delete();
        createIndex();
        get();
        client.shutdown();
        System.exit(5);
    }

    static void createIndex() {
        IndexOperations indexOperations = template.indexOps(IndexCoordinates.of("es_db5"));
        Map<String, Object> settings = new LinkedHashMap<>();
        Map<String, Object> index = new LinkedHashMap<>();
        index.put("analysis.analyzer.default.type", "ik_max_word");
        settings.put("index", index);

        Map<String, Map<String, Object>> map = new LinkedHashMap<>();
        Map<String, Object> name = new LinkedHashMap<>();
        name.put("type", "keyword");
        name.put("index", true);
        name.put("store", true);
        map.put("name", name);

        Map<String, Object> sex = new LinkedHashMap<>();
        sex.put("type", "integer");
        sex.put("index", true);
        sex.put("store", true);
        map.put("sex", sex);

        Map<String, Object> age = new LinkedHashMap<>();
        age.put("type", "integer");
        age.put("index", true);
        age.put("store", true);
        map.put("age", age);

        Map<String, Object> book = new LinkedHashMap<>();
        book.put("type", "text");
        book.put("index", true);
        book.put("store", true);
        map.put("book", book);

        Map<String, Object> address = new LinkedHashMap<>();
        address.put("type", "text");
        address.put("index", true);
        address.put("store", true);
        map.put("address", address);

        Map<String, Object> properties = new HashMap<>();
        properties.put("properties", map);
        Document document = Document.from(properties);

        indexOperations.create(settings, document);
    }

    static void delete() {
        boolean b = template.indexOps(IndexCoordinates.of("es_db5")).delete();
        System.out.println(b);
    }

    static void get() {
        IndexOperations ops = template.indexOps(IndexCoordinates.of("es_db5"));
        System.out.println(ops.getMapping());
        System.out.println(ops.getSettings());
    }

}
